package com.sense.kukoo.biz_media.adapter.provider

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.google.android.material.progressindicator.CircularProgressIndicator
import com.sense.kukoo.biz_media.adapter.MediaHistoryAdapter
import com.sense.kukoo.common.media.bean.LocalMediaListItemBean
import com.sense.kukoo.common.media.watcher.UserWatcher
import com.sense.kukoo.common.repo.biz.MediaHistoryItemBean
import com.sense.kukoo.common.repo.http.bean.ListenRecordBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder
import com.sense.kukoo.common.wiget.dialog.DeletePopupWindow
import com.sense.kukoo.lib_picture.loader.Monet

class MediaHistoryItemProvider(
    val listener: MediaHistoryAdapter.MediaHistoryEventListener
): MultiItemProvider<MediaHistoryItemBean>() {

    private val TAG: String = javaClass.simpleName
    private var mCurrentMusic: LocalMediaListItemBean? = null
    private var mCurrentMusicState: Int = UserWatcher.MusicStateWatcher.STATE_NOT_PLAYING
    private var mCurrentMusicProgress: Int = 0

    private var dp8 = ApplicationContext.dp2px(8F)

    override fun getItemType(): Int = 0

    override fun getItemLayoutId(): Int = com.sense.kukoo.common.R.layout.common_item_media_container

    override fun onBindViewHolder(viewHolder: BaseViewHolder, item: MediaHistoryItemBean) {
        val data: ListenRecordBean = item.data
        //
        //
        val iv_music_icon = viewHolder.getView(com.sense.kukoo.common.R.id.iv_music_icon) as ImageView
        Monet.get(iv_music_icon.context)
            .load<Any>(data.coverUrl)
            .centerCropWithConner(dp8)
            .into(iv_music_icon)
        //
        val tv_title = viewHolder.getView(com.sense.kukoo.common.R.id.tv_title) as TextView
        tv_title.text = data.title
        //
        val tv_time_left = viewHolder.getView(com.sense.kukoo.common.R.id.tv_time_left) as TextView
        var len:Int = data.left ?: 0
        len = len/60
        len = if(len <=0 ) 1 else len
        tv_time_left.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.library_min_left, len)
        //
        val iv_btn_play = viewHolder.getView(com.sense.kukoo.common.R.id.iv_btn_play) as ImageView
        val cpi_btn_progress = viewHolder.getView(com.sense.kukoo.common.R.id.cpi_btn_progress) as CircularProgressIndicator
        if(data.contentId == (mCurrentMusic?.idOfChapter() ?: 0)){
            //当前播放的音乐
            cpi_btn_progress.visibility = View.VISIBLE
            cpi_btn_progress.progress = mCurrentMusicProgress
            when (mCurrentMusicState) {
                UserWatcher.MusicStateWatcher.STATE_PREPARE,
                UserWatcher.MusicStateWatcher.STATE_ERROR_RETRYING,
                UserWatcher.MusicStateWatcher.STATE_SEEKING,
                UserWatcher.MusicStateWatcher.STATE_PLAYING ->{
                    iv_btn_play.setImageResource(com.sense.kukoo.common_design.R.drawable.design_icon_playbutton_pause_40dp)
                }
                else ->{
                    iv_btn_play.setImageResource(com.sense.kukoo.common_design.R.drawable.design_icon_playbutton_play_40dp)
                }
            }
        } else {
            //非当前
            cpi_btn_progress.visibility = View.INVISIBLE
            iv_btn_play.setImageResource(com.sense.kukoo.common_design.R.drawable.design_icon_playbutton_start_40dp)
        }
        iv_btn_play.setOnClickListener {
            listener.onMediaPlayClick(item)
        }
        //
        val cl_card_content = viewHolder.getView(com.sense.kukoo.common.R.id.cl_card_content)
        cl_card_content.setOnLongClickListener { card ->
            val location = IntArray(2)
            card.getLocationInWindow(location)
            val locationY = location[1]
            val isUp: Boolean = locationY > 3 * card.height
            //
            DeletePopupWindow.autoShow(card.context, card, isUp){
                listener.onMediaDelete(item)
            }
            //
            return@setOnLongClickListener true
        }
    }

    fun setCurrentMusic(music: LocalMediaListItemBean){
        mCurrentMusic = music
    }

    fun setCurrentPlayState(state: Int, progress: Int){
        mCurrentMusicState = state
        mCurrentMusicProgress = progress
    }
}